package com.atguigu.educms.controller;


import com.atguigu.com.R;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-01-05
 */
@RestController
@RequestMapping("/educms/banneradmin")
public class BannerAdminController {
    @Autowired
    private CrmBannerService crmBannerService;
    //后台 分页
    @GetMapping("{page}/{limit}")
    public R PageBanner(@PathVariable("page") long page,@PathVariable("limit") Long limit){
        Page<CrmBanner> bannerPage = new Page<>(page,limit);
        IPage<CrmBanner> iPage = crmBannerService.page(bannerPage, null);
        List<CrmBanner> records = iPage.getRecords();
        long total = iPage.getTotal();
        return R.ok().data("records",records).data("total",total);
    }

    //添加
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.save(crmBanner);
        return R.ok();
    }
    //根据id获取Banner
    @GetMapping("getBannerById/{id}")
    public R getBannerById(@PathVariable("id") String id){
        CrmBanner banner = crmBannerService.getById(id);
        return R.ok().data("banner",banner);
    }
    //根据ID删除
    @DeleteMapping("deletedBanner/{id}")
    public R deleteBanner(@PathVariable String id){
        crmBannerService.removeById(id);
        return R.ok();
    }
    //修改
    @PostMapping("update")
    public R update(@RequestBody CrmBanner crmBanner){
        crmBannerService.update(crmBanner,null);
        return R.ok();
    }


}

